package com.group.three.controller.order;


import com.group.three.entity.AfterSale;
import com.group.three.service.AfterSaleService;
import com.group.three.vo.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * 店铺端,售后管理接口
 */
@RestController
@Api(tags = "店铺端,售后管理接口")
@RequestMapping("/store/order/afterSale")
public class AfterSaleStoreController {
    @Autowired
    private AfterSaleService afterSaleService;

    @ApiOperation(value = "分页获取售后服务")
    @GetMapping(value = "/page")
    public JsonResult getByPage(String storeId, int pageNum, int pageSize) {

        List<AfterSale> afterSaleItem = afterSaleService.getAfterSalePages(storeId, pageNum, pageSize);
        if (afterSaleItem != null) {
            return new JsonResult("200", "售后服务列表查询成功", afterSaleItem);
        }
        return new JsonResult("400", "售后服务列表查询失败");
    }

    @ApiOperation(value = "审核售后申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "afterSaleSn", value = "售后sn", required = true, paramType = "path"),
            @ApiImplicitParam(name = "serviceStatus", value = "PASS：审核通过，REFUSE：审核未通过", required = true, paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", paramType = "query"),
            @ApiImplicitParam(name = "actualRefundPrice", value = "实际退款金额", paramType = "query")
    })
    @PutMapping(value = "/review/{afterSaleSn}")
    public JsonResult review(@NotNull(message = "请选择售后单") @PathVariable String afterSaleSn,
                             @NotNull(message = "请审核") String serviceStatus,
                             String remark,
                             Double actualRefundPrice) {

        int i = afterSaleService.review(afterSaleSn, serviceStatus, remark, actualRefundPrice);
        if (i == 1) {
            return new JsonResult("200", "审核售后表成功");
        }
        return new JsonResult("400", "审核售后表失败");
    }

    @ApiOperation(value = "卖家确认收货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "afterSaleSn", value = "售后sn", required = true, paramType = "path"),
            @ApiImplicitParam(name = "serviceStatus", value = "PASS：审核通过，REFUSE：审核未通过", required = true, paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", paramType = "query")
    })
    @PutMapping(value = "/confirm/{afterSaleSn}")
    public JsonResult confirm(@NotNull(message = "请选择售后单") @PathVariable String afterSaleSn,
                              @NotNull(message = "请审核") String serviceStatus,
                              String remark) {
        int i = afterSaleService.storeConfirm(afterSaleSn, serviceStatus, remark);
        if (i == 1) {
            return new JsonResult("200", "商家审核售后确认收货成功");
        }
        return new JsonResult("400", "商家审核售后确认收货失败");

    }


    @ApiOperation(value = "商家换货业务发货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "afterSaleSn", value = "售后sn", required = true, paramType = "path"),
            @ApiImplicitParam(name = "changeStatus", value = "PASS：已发货，REFUSE：未发货", required = true, paramType = "query"),
            @ApiImplicitParam(name = "logisticsSn", value = "物流单号", required = true, paramType = "path"),
            @ApiImplicitParam(name = "logisticsCompany", value = "物流公司", required = true, paramType = "path")
    })
    @GetMapping(value = "/delivery")
    public JsonResult change(@NotNull(message = "请选择售后单")  String afterSaleSn,
                             @NotNull(message = "请审核") String changeStatus,
                             String logisticsSn, String logisticsCompany) {
        int i = afterSaleService.changeLogistics(afterSaleSn, changeStatus, logisticsSn,logisticsCompany);
        if (i == 1) {
            return new JsonResult("200", "商家发货成功");
        }
        return new JsonResult("400", "商家发货失败");

    }
}
